package com.etime.service.impl;

import com.etime.mapper.UserMapper;
import com.etime.pojo.User;
import com.etime.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

@Service("us")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> getAllUser() {
        List<User> list=userMapper.getAllUser();
        return list;
    }

    @Override
    public boolean login(User user, HttpSession session) {
        User resultUser=userMapper.login(user);
        if(resultUser == null){
            return false;
        }else{
            session.setAttribute("loginUser",resultUser);
            return true;
        }
    }

    @Override
    public PageInfo<User> getUserByPage(Integer page) {
        //提供页码，提供每页显示的数据条数
        PageHelper.startPage(page,5);
        //执行查询该表所有数据的方法
        List<User> list=userMapper.getAllUser();
        //封装返回的数据
        PageInfo<User> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<User> getUserByName(Integer page, String user_name) {
        //提供页码，提供每页显示的数据条数
        PageHelper.startPage(page,5);
        //执行查询该表所有数据的方法
        List<User> list=userMapper.getUserByName("%"+user_name+"%");
        //封装返回的数据
        PageInfo<User> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public boolean register(User user) {
        int rows=userMapper.register(user);
        if(rows==0) {
            return false;
        }else{
            return true;
        }
    }

    @Override
    public boolean userLogin(User user, HttpSession session) {
        User resUser = userMapper.userLogin(user);
        if(resUser == null){
            return false;
        }else{
            session.setAttribute("resUser",resUser);
            return true;
        }
    }

    @Override
    public User getUserById(Integer user_id) {
        return userMapper.getUserById(user_id);
    }

    @Override
    public boolean updateUser(User user) {
        int rows = userMapper.updateUser(user);
        if (rows == 0) {
            return false;
        } else {
            return true;
        }

    }
}
